package com.greate.spike.springbootspikedemo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @ClassName：RedisConfig
 * @Description：Redis配置类
 * @Author：chenyb
 * @Date：2020/8/16 11:48 下午
 * @Versiion：1.0
 */
@Configuration //当前类为配置类
public class RedisConfig {

	@org.springframework.context.annotation.Bean //redisTemplate注入到Spring容器
	public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
		RedisSerializer<String> redisSerializer = new StringRedisSerializer();
		redisTemplate.setConnectionFactory(factory);
		//key序列化
		redisTemplate.setKeySerializer(redisSerializer);
		//value序列化
		redisTemplate.setValueSerializer(redisSerializer);
		//value hashmap序列化
		redisTemplate.setHashKeySerializer(redisSerializer);
		//key hashmap序列化
		redisTemplate.setHashValueSerializer(redisSerializer);
		return redisTemplate;
	}
}
